package com.fungame.wordsearch;

import java.awt.Point;
import java.io.Console;
import java.text.ParseException;
import java.util.StringTokenizer;

/**
 * This class is .....
 *
 */
public class WordSearch 
{
    public static void main( String[] args )
    {
    	Console console = System.console();
    	int row = Integer.parseInt(console.readLine("[INPUT] : Enter number of rows : "));
    	int column = Integer.parseInt(console.readLine("[INPUT] : Enter number of columns : "));
    	int wordLength = Integer.parseInt(console.readLine("[INPUT] : Enter number of words : "));
    	WordSearch ws=new WordSearch();
    	Word [] inputWordList=new Word[wordLength];
    	Word inputWord;
    	for(int i=0;i<wordLength;i++)
    	{
    		inputWord = new Word(console.readLine("[INPUT] : Enter Word ("+(i+1)+") : "));
    		inputWordList[i]=inputWord;
    	}
    	
    	
    	try {
			ws.initialize(inputWordList, row, column);
			StringTokenizer inputBreaker=null;
			String startcoordinate = null;
			String endcoordinate = null;
			Point startPoint = null;
			Point endPoint = null;
			System.out.println("[Message] : Row and Column number start with 0");
			while(!ws.allWordsFound())
			{
				try{
				
				String  inputCoordinate  = console.readLine("[INPUT] : Enter coordinates in grid to search word (Eg . : 1,1-3,5 ): ");
				inputBreaker=new StringTokenizer(inputCoordinate,"-");
				startcoordinate = inputBreaker.nextToken();
				endcoordinate = inputBreaker.nextToken();
				inputBreaker = new StringTokenizer(startcoordinate,",");
				startPoint=new Point(Integer.parseInt(inputBreaker.nextToken()),Integer.parseInt(inputBreaker.nextToken()));
				inputBreaker = new StringTokenizer(endcoordinate,",");
				endPoint=new Point(Integer.parseInt(inputBreaker.nextToken()),Integer.parseInt(inputBreaker.nextToken()));
				if(ws.checkWord(startPoint, endPoint))
				{
					System.out.println("[Message] : Word Found");
				}
				else
				{
					System.out.println("[Message] : Word not found");
				}
				}
				catch (Exception e) {
					System.out.println("[ERROR] : Invalid input");
				}
				ws.grid.display(inputWordList);
				
			}
		} catch (WordGridException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("[ERROR] : Cannot place the words in given grid size. Please increase the grid size.");
		}
		catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("[ERROR] : Input Error.");
		}	
    	catch (ParseException e) {
    		System.out.println("[ERROR] : Input Error.");
		}
    	catch (Exception e) {
			// TODO: handle exception
		}
    	
    	
    	
    	
    	
    	    	
    	
    	
    }
    
    Word [] wordList;
    WordGrid grid;
    
    public void initialize(Word [] wordList , int rows , int column) throws Exception
    {
    	this.wordList=wordList;
    	grid=new WordGrid();
    	grid.init(rows, column);
    	grid.fill(wordList);
    	grid.display(wordList);
    }
 
    
   
    
    public boolean checkWord(Point start,Point end)
	{
    	//if any word has start and end point mentioned in .. its a hit.. highlight it
    	Word word=null;
    	Point wordStart; 
		Point wordEnd ;
		boolean wordFound= false;
    	for (int i = 0; i < wordList.length; i++) {
    		word=wordList[i];
    		wordStart = word.getStart();
    		wordEnd = word.getEnd();
    		//System.out.println(word.getWord()+ " : "+wordStart.getX()+" , "+wordS);
    		if(wordStart.getX()==start.getX()&&wordStart.getY()==start.getY()&&wordEnd.getX()==end.getX()&&wordEnd.getY()==end.getY())
    		{
    			word.setFound(true);
    			wordFound = true;
    			grid.highlight(word);
    			break;
    		}
			
		}
    	
    	
		return wordFound;
	}
	
	public boolean allWordsFound()
	{
		boolean isAllWordFound=true;
		Word word=null;
		for (int i = 0; i < wordList.length; i++) {
    		word=wordList[i];
    		if(!word.isFound())
    		{
    			isAllWordFound=false;
    			break;
    		}
			
		}
		return isAllWordFound;
	}
    
    
    
}
